package cn.lkk.samples.lang.fundamental.Operators;

import static cn.lkk.utils.PrintUtils.printExpressionResult;

/**
 * Created by lkk on 2015/5/11.
 */
public class BitOperators {
    public static void main(String[] args) {
        // x=11010,y=10101
        int x = 26, y = 21, z = -26;
        System.out.printf("x = %1$s %ny = %2$s %nz = %3$s %n", Integer.toBinaryString(x), Integer.toBinaryString(y), Integer.toBinaryString(z));

        int and = x & y;
        int or = x | y;
        int xor = x ^ y;
        int not = ~x;
        int ls = x << 1;
        printExpressionResult("x & y", and);
        printExpressionResult("x | y", or);
        printExpressionResult("x ^ y", xor);
        printExpressionResult("~x", not);
        printExpressionResult("x<<1", ls);


        //右移，高位补符号位
        int rsp = x >> 1;
        int rsn = z >> 1;
        //右移，高位补0
        int rszp = x >>> 1;
        int rszn = z >>> 1;
        printExpressionResult("x >> 1", Integer.toBinaryString(rsp));
        printExpressionResult("z >> 1", Integer.toBinaryString(rsn));
        printExpressionResult("x >>> 1", Integer.toBinaryString(rszp));
        printExpressionResult("x >>> 1", Integer.toBinaryString(rszn));

    }
}
